<?php

/**
 * alert actions.
 *
 * @package    ringBackend
 * @subpackage alert
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class alertActions extends sfActions
{
	/**
	 * Executes index action
	 *
	 */
	public function executeIndex()
	{
		//$this->forward('default', 'module');
	}
    public function executeDailyDownloadContent()
	{
            $this->type  = $this->getRequestParameter('type',0);
            $this->startDate  = $this->getRequestParameter('start_date',null);
            $this->endDate  = $this->getRequestParameter('end_date',null);
            $this->price = $this->getRequestParameter('price',3000);
            $this->sourceType  = $this->getRequestParameter('source_type',0);
            $this->category  = $this->getRequestParameter('category',0);
            $this->categories = RingtoneCategoryPeer::getListCategory(new Criteria());
            if ($this->getRequest()->getMethod() == sfRequest::POST){
                if ($this->startDate == null){
                    $startDate = null;
                }else{
                    $startDate = date("Y-m-d",strtotime($this->startDate));
                    if ($startDate == false || $startDate == -1){
                        $startDate = null;
                    }
                }
                if ($this->endDate == null){
                    $endDate = null;
                }else{
                    $endDate = date("Y-m-d",strtotime($this->endDate));
                    if ($endDate == false || $endDate == -1){
                        $endDate = null;
                    } 
                }
                $this->ringtoneDownloads = RingtoneDownloadPeer::getDailyRingtoneDowload($this->type,$this->sourceType, $startDate, $endDate, $this->price, $this->category);
                               
                /*
                 * Ghi du lieu ra file json de hien thi tren bieu do thong ke
                 */
                $rtDownloads = $this->ringtoneDownloads;
                if (count($rtDownloads) > 0) {	                
	                $maxTotalMoney = 0;
	                $arrDate = array();
	                $arrTotalMoney = array();	                 
	                
					for ($i = count($rtDownloads)-1; $i>=0; $i=$i-1){
						$arrDate[] = date("d/m", strtotime($rtDownloads[$i]['dateTime']));
						$arrTotalMoney[] = (int)$rtDownloads[$i]['totalMoney'];
						$maxTotalMoney = ($rtDownloads[$i]['totalMoney'] > $maxTotalMoney) ? $rtDownloads[$i]['totalMoney'] : $maxTotalMoney;
					}
	                           
	                chdir(dirname(__FILE__));
	                require_once('../lib/ofc-library/lib/OFC/OFC_Chart.php');
	             	$title = new OFC_Elements_Title( "Our New House Schedule" );
	             					
					srand((double)microtime()*1000000);					
									
					$line = new OFC_Charts_Line();
					$line->set_width( 1 );
					$line->set_colour( '#5E4725' );
					$line->set_dot_size( 5 );
					$line->set_values( $arrTotalMoney );
					
					$x = new OFC_Elements_Axis_X();
					$x->set_offset( false );
					$x->set_labels_from_array($arrDate);
					
					
					$y = new OFC_Elements_Axis_Y();
					$y->set_range( 0, $maxTotalMoney + 100000, 100000 );
					
					$title = new OFC_Elements_Title( 'Biểu đồ doanh thu' );
					$title->set_style( "{font-size: 20px;}" );
					
					$chart = new OFC_Chart();				
					$chart->set_title( $title );
					$chart->set_y_axis( $y );
					$chart->set_x_axis( $x );								
					$chart->add_element( $line );
					
					$filePath = '/srv/www/htdocs/iringBackend/web/flash/charts_data/daily'. '_' . sfContext::getInstance()->getUser()->getGuardUser()->getUsername() . '.json';
					$fh = fopen($filePath, 'w');
					fwrite($fh, $chart->toPrettyString());
					fclose($fh);
				}			
            }
	}
	
	public function executeMonthlyDownloadContent()
	{
            $this->type  = $this->getRequestParameter('type',0);
            //$this->startDate  = $this->getRequestParameter('start_date',null);
            //$this->endDate  = $this->getRequestParameter('end_date',null);
            $this->price = $this->getRequestParameter('price',3000);
            $this->sourceType  = $this->getRequestParameter('source_type',0);
            if ($this->getRequest()->getMethod() == sfRequest::POST){
                /*
            	if ($this->startDate == null){
                    $startDate = null;
                }else{
                    $startDate = date("Y-m-d",strtotime($this->startDate));
                    if ($startDate == false || $startDate == -1){
                        $startDate = null;
                    }
                }
                if ($this->endDate == null){
                    $endDate = null;
                }else{
                    $endDate = date("Y-m-d",strtotime($this->endDate));
                    if ($endDate == false || $endDate == -1){
                        $endDate = null;
                    } 
                }*/
                $this->ringtoneDownloads = RingtoneDownloadPeer::getMonthlyRingtoneDowload($this->type,$this->sourceType,$this->price);
                /*
                 * Ghi du lieu ra file json de hien thi tren bieu do thong ke
                 */
                $rtDownloads = $this->ringtoneDownloads;
                if (count($rtDownloads) > 0) {	                
	                $maxTotalMoney = 0;
	                $arrDate = array();
	                $arrTotalMoney = array();	                 
	                
					for ($i = count($rtDownloads)-1; $i>=0; $i=$i-1){
						$arrDate[] = $rtDownloads[$i]['month'] . '/' . $rtDownloads[$i]['year'];
						$arrTotalMoney[] = (int)$rtDownloads[$i]['totalMoney'];
						$maxTotalMoney = ($rtDownloads[$i]['totalMoney'] > $maxTotalMoney) ? $rtDownloads[$i]['totalMoney'] : $maxTotalMoney;
					}
	                           
	                chdir(dirname(__FILE__));
	                require_once('../lib/ofc-library/lib/OFC/OFC_Chart.php');
	             	$title = new OFC_Elements_Title( "Our New House Schedule" );
	             					
					srand((double)microtime()*1000000);					
									
					$line = new OFC_Charts_Line();
					$line->set_width( 1 );
					$line->set_colour( '#5E4725' );
					$line->set_dot_size( 5 );
					$line->set_values( $arrTotalMoney );
					
					$x = new OFC_Elements_Axis_X();
					$x->set_offset( false );
					$x->set_labels_from_array($arrDate);
					
					
					$y = new OFC_Elements_Axis_Y();
					$y->set_range( 0, $maxTotalMoney + 1000000, 1000000 );
					
					$title = new OFC_Elements_Title( 'Biểu đồ doanh thu' );
					$title->set_style( "{font-size: 20px;}" );
					
					$chart = new OFC_Chart();				
					$chart->set_title( $title );
					$chart->set_y_axis( $y );
					$chart->set_x_axis( $x );								
					$chart->add_element( $line );
						
					$filePath = '/srv/www/htdocs/iringBackend/web/flash/charts_data/monthly'. '_' . sfContext::getInstance()->getUser()->getGuardUser()->getUsername() . '.json';	                
					$fh = fopen($filePath, 'w');
					fwrite($fh, $chart->toPrettyString());
					fclose($fh);
				}			
            }
	}
	
	
	public function executeGetSQLString()
	{
		$type  = $this->getRequestParameter('type');
		$sqlStr = new SqlStatistic();
		$sqlStr->setId($type);
		$sqlStr = SqlStatisticPeer::doSelectOne($sqlStr->buildCriteria());
		if ($sqlStr)
		{
			echo  $sqlStr->getSqlString();
		}
		else
		{
			echo "Chưa có câu lệnh SQL, hãy tự viết!!";
		}
		exit();
	}

	public function executeSaveAsSQL()
	{
		$asName  = $this->getRequestParameter('asName');
		$id  = $this->getRequestParameter('id');
		$sqlString  = $this->getRequestParameter('sqlString');
			
		$retVal = new stdClass();
		$retVal->success = true;
		$retVal->message = "";
		$retVal->newItem = false;
		$retVal->name = $asName;

		$sql = new SqlStatistic();
		if ($asName != null)
		{
			if (!SqlStatisticPeer::isExisted($asName))
			{
				$sql->setNew(true);;
				$sql->setName($asName);
				if ($sqlString!=null) {
					$sql->setSqlString($sqlString);
				}
				$sql->save();
				$retVal->id = $sql->getId();
				$retVal->success = true;
				$retVal->newItem = true;
			}
			else
			{
				$sql->setName($asName);
				$sql = SqlStatisticPeer::doSelectOne($sql->buildCriteria());
				$retVal->success = false;
				$retVal->message = " Báº¡n pháº£i ghi ra tÃªn khÃ¡c, tÃªn nÃ y Ä‘Ã£ tá»“n táº¡i";
			}
		}
		elseif ( $id != null)
		{
			$sql->setId($id);
			$sql = SqlStatisticPeer::doSelectOne($sql->buildCriteria());
			$sql->setSqlString($sqlString);
			$sql->save();
			$retVal->id = $id;
			$retVal->success = true;
			$retVal->newItem = false;
		}
		
		echo FastJSON::encode($retVal);
		exit();
	}

	/**
	 * THis action used to get Common statistic
	 *
	 */
	public function executeCommonStatistic()
	{
		if ($this->getRequest()->getMethod() != sfRequest::POST)
		{
			$this->sqlString = null;
			$this->permission = true;
			$this->sqlLists = SqlStatisticPeer::getSqlList();
		}
		else
		{
			$this->sqlLists = SqlStatisticPeer::getSqlList();
			$sqlString  = $this->getRequestParameter('sqlString');
			$this->optValue = $this->getRequestParameter('optStatisticType');
			$this->sqlString = $sqlString;
			if ($sqlString != null)
			{
				$sqlString = trim($sqlString);
				$subSql = substr($sqlString, 0, 6);
				if ( strtolower($subSql) == "select")
				{
					$this->result = AdminPeer::getUserStatistic($sqlString);
					$this->permission = true;
				}
				else
				{
					$this->permission = false;
				}
			}
		}

		$this->setLayout(false);
	}

}
